package com.cloudsigns.factorymanager2.produce.model;

import android.util.Log;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class GetProductWorkPartListModel implements IGetProductWorkPartListModel{

    @Override
    public void getProductWorkPartList(final OnGetProductWorkPartListListListener onGetProductWorkPartListListListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                HttpClient client = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(ProjectUtil.GET_PRODUCT_WORK_PART_LIST);
                HttpResponse response;
                try {
                    httpPost.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpPost.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);
                    response = client.execute(httpPost);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    Log.e("GetProductWorkPartListM", "result=" + result);
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != onGetProductWorkPartListListListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray jsonArr = new JSONArray(json.getString("workPartTypeResponseList"));
                            List<WorkPartBean> workPartList = new ArrayList< >();
                            if (jsonArr != null) {
                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject jsonObj = jsonArr.getJSONObject(i);
                                    if (jsonObj != null) {
                                        WorkPartBean workPartBean = new WorkPartBean();
                                        workPartBean.setWorkPartName(jsonObj.getString("typeName"));
                                        workPartList.add(workPartBean);
                                    }
                                }
                                onGetProductWorkPartListListListener.getDataSuccessed(workPartList);
                            }
                        } else {
                            onGetProductWorkPartListListListener.getDataFailed(code + "");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface OnGetProductWorkPartListListListener {
        void getDataSuccessed(List<WorkPartBean> productOrderBeanList);

        void getDataFailed(String desc);
    }
}
